<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="${m.di.pkg}.${m.di.className}" >
	<!-- 基本数据映射 -->
    <resultMap id="BaseResultMap" type="${m.ei.pkg}.${m.ei.className}">
    	<#list m.ei.fields as field>
    	<result column="${field.column.name}" property="${field.name}" javaType="${field.javaTypePath}" />
    	</#list>
    </resultMap>


    <!-- 基本sql片段 -->
    <!-- 全部字段 -->
    <sql id="sql_all_columns">
    	<#list m.ei.fields as field >`${field.name}`<#if field?has_next>, </#if></#list>
    </sql>
    <!-- query生成where子句 -->
    <sql id="sql_query_where">
        <trim prefixOverrides="and">
            1 = 1
			<#list m.ei.fields as field>
            <if test="query.${field.name} != null">and `${field.column.name}` = ${r"#{query."}${field.name}${r"}"}</if>
            <if test="query.${field.name}In != null">and `${field.column.name}` in (<foreach collection="query.${field.name}In" item="item" separator=",">${r"#{"}item${r"}"}</foreach>)</if>
            <#if field.javaTypeText>
            <if test="query.${field.name}XLike != null">and `${field.column.name}` like '%${r"${query."}${field.name}${r"XLike}"}%'</if>
            <if test="query.${field.name}XLeft != null">and `${field.column.name}` like '${r"${query."}${field.name}${r"XLeft}"}%'</if>
            <if test="query.${field.name}XRight != null">and `${field.column.name}` like '%${r"${query."}${field.name}${r"XRight}"}'</if>
            </#if>
            <#if field.javaTypeComparable>
            <if test="query.${field.name}Gt != null">and `${field.column.name}` <![CDATA[>]]> ${r"#{query."}${field.name}${r"Gt}"}</if>
            <if test="query.${field.name}Gte != null">and `${field.column.name}` <![CDATA[>=]]> ${r"#{query."}${field.name}${r"Gte}"}</if>
            <if test="query.${field.name}Lt != null">and `${field.column.name}` <![CDATA[<]]> ${r"#{query."}${field.name}${r"Lt}"}</if>
            <if test="query.${field.name}Lte != null">and `${field.column.name}` <![CDATA[<=]]> ${r"#{query."}${field.name}${r"Lte}"}</if>
            </#if>

            </#list>
        </trim>
    </sql>
    <!-- entity生成insert字段 -->
    <sql id="sql_entity_insert">
        <trim prefixOverrides=",">
        <#list m.ei.fields as field>
        <if test="entity.${field.name} != null">, ${r"#{"}entity.${field.name}${r"}"} </if><if test="entity.${field.name} == null">,null </if>
        </#list>
        </trim>
    </sql>
    <!-- entity生成的update字段 -->
    <sql id="sql_entity_update">
        <trim prefixOverrides=",">
        <#list m.ei.fields as field>
        <if test="entity.${field.name} != null">, `${field.column.name}` = ${r"#{entity."}${field.name}${r"}"}</if>
        </#list>
        </trim>
    </sql>


	<!-- 查询相关 -->
    <select id="findByQuery" resultMap="BaseResultMap">
        select
        <include refid="sql_all_columns" ></include>
        from `${m.tableInfo.name}`
        <where>
            <include refid="sql_query_where"></include>
        </where>
    </select>
    <select id="findByQueryPartField" resultMap="BaseResultMap">
        select
        <foreach collection="fields" item="field" separator=",">${r"${field.sql}"}</foreach>
        from `${m.tableInfo.name}`
        <where>
            <include refid="sql_query_where"></include>
        </where>
    </select>
    
    
    <!-- 分页相关 -->
    <select id="countByQuery" resultType="java.lang.Integer">
        select count(*) from `${m.tableInfo.name}`
        <where>
            <include refid="sql_query_where"></include>
        </where>
    </select>
    <select id="findByPage" resultMap="BaseResultMap">
        select
        <include refid="sql_all_columns" ></include>
        from `test`
        <where>
            <include refid="sql_query_where"></include>
        </where>
        limit ${r"#{page.limit}"} offset ${r"#{page.offset}"}
    </select>
    <select id="findByPagePartField" resultMap="BaseResultMap">
        select
        <foreach collection="fields" item="field" separator=",">${r"${field.sql}"}</foreach>
        from `${m.tableInfo.name}`
        <where>
            <include refid="sql_query_where"></include>
        </where>
        limit ${r"#{page.limit}"} offset ${r"#{page.offset}"}
    </select>
    
    
    <!-- 聚合 -->
	<select id="aggregateByQuery" resultMap="BaseResultMap" >
		select
		<foreach collection="fields" item="field" separator=",">${r"${field.sql}"}</foreach>
        from `${m.tableInfo.name}`
        <where>
            <include refid="sql_query_where"></include>
        </where>
        <if test="groupBy != null">
        	${r"${groupBy.sql}"}
        </if>
	</select>


    <!-- insert -->
    <insert id="insert" >
        insert into `${m.tableInfo.name}`
        (<include refid="sql_all_columns"></include>)
        values
        (<include refid="sql_entity_insert"></include>)
    </insert>
    <insert id="multipleInsert" >
        insert into `${m.tableInfo.name}`
        (<include refid="sql_all_columns"></include>)
        values
        <foreach collection="entities" item="entity" separator=",">
        (<include refid="sql_entity_insert"></include>)
        </foreach>
    </insert>


    <!-- update -->
    <update id="update" >
        alter table `${m.tableInfo.name}` update 
        <include refid="sql_entity_update"></include>
        <where>
            <include refid="sql_query_where"></include>
        </where>
    </update>


    <!-- delete-->
    <delete id="delete">
        alter table `${m.tableInfo.name}` delete
        <where>
            <include refid="sql_query_where"></include>
        </where>
    </delete>

</mapper>